package com.example.back.service.impl;

import com.example.back.constants.CheckupReserveStatus;
import com.example.back.dto.resp.CheckupProductList;
import com.example.back.dto.resp.CheckupReserveList;
import com.example.back.mapper.CheckupReserveMapper;
import com.example.back.service.CheckupReserveService;
import com.example.common.entity.CheckupReserve;
import com.example.common.support.ApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CheckupReserveServiceImpl
 * @Author wengjt
 * @Date 2021/5/8 13:08
 * @Description
 **/
@Service
public class CheckupReserveServiceImpl implements CheckupReserveService {
    @Resource
    private CheckupReserveMapper checkupReserveMapper;

    /**
     * 多条件查询体检预约列表
     *
     * @Author wengjt
     * @Date 13:13 2021/5/8
     */
    @Override
    public CheckupReserveList getCheckupReserveList(String cpName, Long ctId, int[] rStatus, Integer pageNum, Integer pageSize) {
        CheckupReserveList result = new CheckupReserveList();
        PageInfo<CheckupReserve> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            checkupReserveMapper.multipleSearch(cpName, ctId, rStatus);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 获取单个体验预约订单详情
     *
     * @Author wengjt
     * @Date 13:14 2021/5/8
     */
    @Override
    public CheckupReserve getInfo(String crOrderNo) {
        return checkupReserveMapper.findOneById(crOrderNo);
    }

    /**
     * 删除已经处理的预约订单
     *
     * @Author wengjt
     * @Date 13:19 2021/5/8
     */
    @Override
    public Object deleteCheckupReserve(String crOrderNo) {
        CheckupReserve oneById = checkupReserveMapper.findOneById(crOrderNo);
        if (oneById.getRStatus() == CheckupReserveStatus.UN_PROCESS) {
            throw new ApiException(400, "不允许删除未处理的订单");
        }
        oneById.setDisable(1);
        checkupReserveMapper.save(oneById);
        return null;
    }

    /**
     * 处理订单
     *
     * @Author wengjt
     * @Date 13:26 2021/5/8
     */
    @Override
    public Object process(String crOrderNo, Integer rStatus, Date checkup, String checkupAddress, String remark, String refuseReason) {
        CheckupReserve oneById = checkupReserveMapper.findOneById(crOrderNo);
        if (ObjectUtils.isEmpty(oneById)) {
            throw new ApiException(400, "订单不存在");
        }
        if (!oneById.getRStatus().equals(CheckupReserveStatus.UN_PROCESS)) {
            throw new ApiException(400, "该订单已经处理过了");
        }
        if (rStatus.equals(CheckupReserveStatus.AGREE)) {
            oneById.setRStatus(rStatus);
            oneById.setCheckup(checkup);
            oneById.setCheckupAddress(checkupAddress);
            oneById.setRemark(remark);
        } else if (rStatus.equals(CheckupReserveStatus.DISAGREE)) {
            oneById.setRStatus(rStatus);
            oneById.setRemark(remark);
            oneById.setRefuseReason(refuseReason);
        }
        checkupReserveMapper.save(oneById);
        return null;
    }
}
